import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MeinPanel extends JPanel
{
	enum Direction
	{
		RIGHT, DOWN, LEFT, UP
	};

	Direction currentDirection = Direction.UP;
	int x;
	int y;

	public MeinPanel()
	{
		super();

		// this.mitteX = (int)(this.getToolkit().getScreenSize().getWidth()/2);
		// this.mitteY = (int)(this.getToolkit().getScreenSize().getHeight()/2);

		this.x = 100;
		this.y = 100;

	}

	@Override
	public void paint(Graphics g)
	{
		Fibonacci f = new Fibonacci();
		f.calculateNext();
		f.calculateNext();

		g.setColor(Color.RED);
		
		x= 100;
		y = 100;
		
		for (int i = 0; i < 10; i++)
		{
			int fib = f.calculateNext();
			
			switch (this.calculateNextDirection())
			{
			case DOWN:
				x = x - f.getPrevious();
				y = y - f.getCurrent();
				break;
			case LEFT:
				x = x - f.getNext();
				y = y - f.getNext();
				break;
			case UP:
				y = y + f.getNext();
				break;
			case RIGHT:
				x = x + f.getCurrent();
			}
			
			System.out.println((i+2) + " " + fib + " " + x + " " + y + " " + this.currentDirection);
			g.drawRect(x, y, fib, fib);

		}

		// g.setColor(Color.RED);
		// g.fillRect(23, 23, 42, 42);
		//
		// g.setColor(Color.BLUE);
		// g.drawArc(23, 23, 42, 42, 180, 90);
	}

	private Direction calculateNextDirection()
	{

		switch (this.currentDirection)
		{
		case DOWN:
			this.currentDirection = Direction.LEFT;
			break;
		case LEFT:
			this.currentDirection = Direction.UP;
			break;
		case UP:
			this.currentDirection = Direction.RIGHT;
			break;
		case RIGHT:
			this.currentDirection = Direction.DOWN;
			break;
		}

		return this.currentDirection;
	}

	class Fibonacci
	{
		private int m1 = 1;
		private int m2 = 1;
		private int count = 0;

		public int calculateNext()
		{
			count++;
			if (count == 1)
				return m1;
			else if (count == 2)
				return m2;

			int tmp = m2;
			m2 = m1 + m2;
			m1 = tmp;

			return m2;
		}
		
		public int getCurrent()
		{
			return this.m2;
		}
		
		public int getPrevious()
		{
			return this.m1;
		}
		
		public int getNext()
		{
			return this.m1 + this.m2;
		}
		
	}
}
